﻿// JavaScript Document
var authCodeId = '';
//显示隐藏密码
$(function(){
    
    //显示提示
    //showTip("请输入11位手机号码");  
    //显示或隐藏密码
    $(".toggle-pwd").bind("click",function(){
        console.log("hahaha");
        var eyes_btn = $(this);
        var pwd_input = eyes_btn.parents(".pwd-box").find(".login-text");
        if(eyes_btn.hasClass("show")){
            console.log("hohoho");
            eyes_btn.removeClass("show");
            pwd_input.attr("type","password");
        }
        else {
            console.log("hehehe");
            eyes_btn.addClass("show");
            pwd_input.attr("type","text");
        }
    }); 
    
    
    //等待验证码倒计时方法
    var waitingtxt = '<span id="code_timer">60</span>s后重新获取';
    var codeTimer = function() {
        var time_number = $("#code_timer").html()*1;
        time_number--;
        if(time_number<0){
            $("#get_msg_code").removeClass("waiting").html("获取验证码");
            return;
        }
        console.log("time_number:" + time_number);
        $("#code_timer").html(time_number);
        setTimeout(codeTimer,1000);
    };
    var waitingCode = function() {
        var getcode_btn = $("#get_msg_code");
        if(getcode_btn.hasClass("waiting")){
            return false;
        }
        else {
            
            
            //下面添加发送验证码短信请求
            $.post('/authCode/getSMSCode',{
        		pageType:1,
        		mobile:$("input[name=mobile]").val(),
        		imageAuthCode:$("input[name=code]").val(),
        		imageAuthCodeId:authCodeId
        		
        	},function(data){
        		console.log(data)
        		if(data.code == -1){
        			//$("#img-code").attr("src",data.object.imgBase64Src);
        			//authCodeId = data.object.authCodeId;
        			showTip(data.message);
        			return false;
        		}else if(data.code == '201003'){
        			refreshImgCode();
        			$(".phone-code-box").show();
        			showTip("请输入验证码");
        			return false;
        		}else if(data.code == '200'){
        			getcode_btn.html(waitingtxt);
                    getcode_btn.addClass("waiting");
                    $("#code_timer").html("60");
                    codeTimer();
        		}else{
        			showTip(data.message);
        			return false;
        		}
        	})
            //上面添加发送验证码短信请求
        }
    };
    
    
    //点击发送验证码
    $("#get_msg_code").bind("click",function(){
    	var mobile = $("input[name=mobile]").val();
    	if(mobile == '' || mobile.length != 11 ){
    		showTip("请输入11位手机号码");
    		//$("input[name=mobile]").val(trim(mobile));
    		return false;
    	}
    	var code = $("input[name=code]").val();
    	if($(".phone-code-box").height() != 0) {
    		
    		if(code == ''){
    			showTip("请输入验证码");
    			return false;
    		}
    	}
    	
        //如果手机号正确
//        if(true) {
//            //开始倒计时
            waitingCode();
//        }
//        else {
//            //显示错误提示
//        }
    });
    
    //refreshImgCode();
    $("#img-code").on('click',function(){
    	refreshImgCode();
//    	alert(submitObj.authCodeId)
    });
    $(".submit-btn").on('click', function(){
    	var mobile = $("input[name=mobile]").val();
    	if(mobile == '' || mobile.length != 11 ){
    		showTip("请输入11位手机号码");
    		//$("input[name=mobile]").val(trim(mobile));
    		return false;
    	}
    	
    	var msgCode = $("input[name=msgCode]").val();
    	if($(".phone-code-box").height() != 0) {
    		
    		if(msgCode == ''){
    			showTip("请输入手机验证码");
    			return false;
    		}
    	}
    	var pwd = $("input[name=pwd]").val();
    	if(pwd.length < 6 || pwd.length > 12 ){
    		showTip("请输入6至12个字符的密码");
    		return false;
    	}
    	$.post('/new/reg',
    			{
    				mobile:mobile,
    				smsAuthCode:msgCode,
    				password:pwd
    			},
    			function(data){
	    			console.log(data)
	    			if(data.code == '-1'){
	    				showTip(data.message);
	    			}else if(data.code == '101004'){
	    				$(".phone-code-box").show();
	    			}else if(data.code == '200'){
	    				window.location.href=""+data.data;
	    			}else{
	    				showTip(data.message);
	    			}
    		})
    });
});

function refreshImgCode(){
	$.get('/authCode/getImageAuthCode',null,function(data){
		if(data.code == 0){
			$("#img-code").attr("src",data.object.imgBase64Src);
			authCodeId = data.object.authCodeId;
		}
	})
}